﻿using H.Utils;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace LogViewer
{
    public partial class LogViewer : Form
    {
        /// <summary>
        /// 当前打开的文件用于重新加载
        /// </summary>
        private string CurFile;

        public LogViewer()
        {
            InitializeComponent();
        }


        public LogViewer(string logFile) : this()
        {
            LoadFile(logFile);
        }

        private void LoadFile(string logFile)
        {
            try
            {
                using (FileStream fs = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    byte key = (byte)fs.ReadByte();
                    StreamReader sr = new StreamReader(fs.StreamXor(key));
                    TB_Main.Text = sr.ReadToEnd();
                    if (string.IsNullOrEmpty(TB_Main.Text))
                    {
                        TB_Main.Text = "日志内容为空";
                        TB_Main.ForeColor = Color.LightGray;
                    }
                    else
                    {
                        TB_Main.ForeColor = Color.Black;
                    }
                    TB_Main.SelectionStart = TB_Main.Text.Length;
                    TB_Main.SelectionLength = 0;
                }
                CurFile = logFile;
                Text = "日志查看器 - " + Path.GetFullPath(CurFile);
            }
            catch (Exception err)
            {
                Console.WriteLine(err);
            }
        }

        private void TB_Main_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                if (files.Length == 0)
                {
                    return;
                }
                LoadFile(files[0]);
            }
        }

        private void TB_Main_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void TSMI_Reload_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream(CurFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte key = (byte)fs.ReadByte();
                using (StreamReader sr = new StreamReader(fs.StreamXor(key)))
                {
                    TB_Main.Text = sr.ReadToEnd();
                }
            }
        }

        private void TSMI_OpenFile_Click(object sender, EventArgs e)
        {
            if (OFD_Main.ShowDialog() == DialogResult.OK)
            {
                LoadFile(OFD_Main.FileName);
            }
        }

        private void TB_Main_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
